#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H

#include <QDialog>
#include <QButtonGroup>
#include <QTime>
#include <QTcpSocket>

#include "qextserialport.h"
#include "qextserialenumerator.h"
#include "controller.h"

struct IniInfo {
    int mCId;
    QString mControl;
    QString mSunLight;
};

struct SerialPort {
    QString mName;
    PortSettings mPortSettings;
    int mRetryCount;
    int mRetryTimeout;  // [msec]
};

struct SocketServer {
    QString mAddress;
    QString mPortNum;
};

struct MiscInfo {
    bool mIsLog;
    int mKeepAlive; // 10 ~ [sec]
    bool mIsHeating;
};

struct Temperature {
    bool mIsUse;
    int mInterval;  // 0 ~ 3600 [sec]
    short mSetValue;
    short mCurValue;
};

struct TimerAction {
    quint8 cid;
    quint8 bid;
    quint8 rid;
    quint8 act;
};

#define DEF_INI_FILENAME    "cubecontrol-v2.ini"
#define DEF_XML_CONTROL     "cubecontrol-v2.xml"
#define DEF_INI_SUNLIGHT    "cubesunlight.ini"

namespace Ui {
class DialogMain;
}

class DialogMain : public QDialog
{
    Q_OBJECT

public:
    explicit DialogMain(QWidget *parent = 0);
    ~DialogMain();

protected:
    virtual void timerEvent(QTimerEvent *e);

private:
    void loadConfig(const QString fileName);
    void saveConfig(const QString fileName);
    void initContextMenu();
    void initWidget();
    void initSerialPort();
    void initTcpSocket();

    void setStatus(int cid, int bid, int rid, int status);
    void sync();
    void reboot();
    void option(QString &packet, int curIdx);
    void testTimer(const QDateTime &dt);

    bool temperatureParsing();

Q_SIGNALS:
    void signalNeedToShow();
    void signalDayChange(const QDateTime &dt);
    void signalMinuteChange(const QDateTime &dt);
    void signalKeepAliveCheck();

public Q_SLOTS:
    void onParseArgs(const QString& args);
    void onLog(const QString log);

private Q_SLOTS:
    void onShowContextMenu(const QPoint& pos);
    void onMenuSerialPort();
    void onMenuSocketServer();
    void onMenuLocalReboot();
    void onMenuServerReboot();
    void onMenuExit();
    void onButtonClicked(int id);
    void onPushDelay(int bid, int rid);

    void onSerialPortOpen();
    void onSerialPortClose();
    void onSerialPortSend();
    void onSerialPortRead();
    void onSerialPortAdded(QextPortInfo portInfo);
    void onSerialPortRemoved(QextPortInfo portInfo);

    void onTcpConnect();
    void onTcpConnected();
    void onTcpRead();
    void onTcpConnectionClosedByServer();
    void onTcpError(QAbstractSocket::SocketError socketError);

    void onDayChanged(const QDateTime &dt);
    void onMinuteChanged(const QDateTime &dt);
    void onKeepAlive();

private:
    Ui::DialogMain *ui;
    QButtonGroup *mpButtonGroup;

    Controller mController;

    int mOneSecondTimerId;
    int mOneSecondTimerCount;

    IniInfo mIniInfo;
    SerialPort mSerialPort;
    SocketServer mSocketServer;
    MiscInfo mMiscInfo;
    Temperature mTemperature;

    QextSerialPort *mpSerialPort;
    QextSerialEnumerator *mpSerialEnumerator;
    QList<PacketData> mSerialPortSendPacket;
    int mSerialPortSendTimerId;
    QByteArray mSerialPortRecvBuf;

    QTcpSocket *mpTcpSoceket;
    bool mIsSocketInit;
    bool mIsSerialInit;
};

#endif // DIALOGMAIN_H
